# Create your views here.
from django.shortcuts import render_to_response
from dev.synchronicity.models import *
from django import newforms as forms

from django.template import Context, loader
from django.http import HttpResponse, HttpResponseRedirect

def index(request):
	ProjectForm = forms.models.form_for_model(Project)

	form = ProjectForm()

	t = loader.get_template('index.html')
	c = Context({
		'form': form.as_p(),
	})
	return HttpResponse(t.render(c))

def add_edit_project(request, id=None):
	"""add a project if no id given
	   otherwise edit the project for id """
	if id is None:
		ProjectForm = forms.models.form_for_model(Project)
	else:
		project = Project.objects.get(id=id)
		ProjectForm = forms.models.form_for_instance(project)
	if request.method == 'POST':
		form = ProjectForm(request.POST)
		if form.is_valid():
			form.clean_data['owner'] = request.user.id
			form.save()
			return HttpResponseRedirect("/")
	else:
		form = ProjectForm()
	# need to create the add_edit_project.html template
	t = loader.get_template('add_edit_project.html')
	c = Context({
		'form': form
	})
	return HttpResponse(t.render(c))


def addObjective(request):
	""" Add an objective """
	ObjectForm = forms.models.form_from_model(Objective)

	form = ObjectForm()

	t = loader.get_template('add_objective.html')
	c = Context({
		'form': form.as_p(),
	})

	return HttpResponse(t.render(c))